import pkg from "../../package.json";
import type { GlobEnvConfig } from "@/types/config";

export function getCommonStoragePrefix() {
	const { VITE_GLOB_APP_SHORT_NAME } = getAppEnvConfig();
	return `${VITE_GLOB_APP_SHORT_NAME}__${getEnv()}`.toUpperCase();
}

// Generate cache key according to version
export function getStorageShortName() {
	return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase();
}

export function getAppEnvConfig() {
	const ENV = import.meta.env as unknown as GlobEnvConfig;

	const { VITE_GLOB_APP_TITLE, VITE_GLOB_APP_SHORT_NAME } = ENV;

	if (!/^[a-zA-Z_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) {
		console.warn(
			"VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running."
		);
	}

	return {
		VITE_GLOB_APP_TITLE,
		VITE_GLOB_APP_SHORT_NAME
	};
}

/**
 * @description: Get environment variables
 * @returns:
 * @example:
 */
export function getEnv(): string {
	return import.meta.env.MODE;
}

/**
 * @description: Is it a development mode
 * @returns:
 * @example:
 */
export function isDevMode(): boolean {
	return import.meta.env.DEV;
}

/**
 * @description: Is it a production mode
 * @returns:
 * @example:
 */
export function isProdMode(): boolean {
	return import.meta.env.PROD;
}
